package com.complex.client.ui;

import com.complex.client.service.gen.WebServiceTreeNode;

import javax.swing.*;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.TableModel;
import javax.swing.tree.TreeModel;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA.
 * User: AGrankin
 * Date: 27.08.13
 * Time: 17:37
 * To change this template use File | Settings | File Templates.
 */
public class MainFrame extends JFrame {

    private JTabbedPane tabbedPane = new JTabbedPane();


    //-------------------- Visual Tree Components---------------------------
    private JPanel visualTreePanel = new JPanel();
    //-------------------- /Visual Tree Components---------------------------


    //-------------------- Tree Components---------------------------
    private JSplitPane swingTreeSplitPane = null;

    private JPanel treeViewPanel = new JPanel();
    private JScrollPane treeViewScrollPane = null;

    private JPanel attributesPanel = new JPanel();
    private JScrollPane attributesScrollPane = null;

    private static TreeModel myTreeModel;
    private static TableModel myTableModel;
    private JTree treeView = new JTree();
    private JTable attributesTable = new JTable();

    private JPanel nodeComponentsPanel = new JPanel();
    private JPanel attributesComponentsPanel = new JPanel();
    private JLabel nodeNameLabel = new JLabel("Node name:");
    private JLabel attributeNameLabel = new JLabel("Attribute name:");
    private JLabel attributeValueLabel = new JLabel("Attribute value:");
    private JTextField nodeNameTextF = new JTextField();
    private JTextField attributeNameTextF = new JTextField();
    private JTextField attributeValueTextF = new JTextField();
    private JRadioButton addNodeRadio = new JRadioButton("Add node");
    private JRadioButton addChildRadio = new JRadioButton("Ann node as child");
    private JButton addNodeButton = new JButton("Add");
    private JButton removeNodeButton = new JButton("Remove");
    private JButton addAttributeButton = new JButton("Add");
    private JButton removeAttributeButton = new JButton("Remove");
    //-------------------- /Tree Components---------------------------

    public MainFrame() {


        treeView.setModel(myTreeModel);
        treeViewScrollPane = new JScrollPane(treeView);
        treeViewScrollPane.setPreferredSize(new Dimension(300, 400));

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(addNodeRadio);
        buttonGroup.add(addChildRadio);
        addNodeRadio.setSelected(true);

        GridBagLayout gridBagLayout = new GridBagLayout();
        treeViewPanel.setLayout(gridBagLayout);
        treeViewPanel.add(treeViewScrollPane, new GBC(0, 0, 2, 1).setWeight(100, 100).setFill(GBC.BOTH));
        treeViewPanel.add(nodeNameLabel, new GBC(0, 1).setWeight(5, 5));
        nodeNameTextF.setMinimumSize(new Dimension(150, 20));
        treeViewPanel.add(nodeNameTextF, new GBC(1, 1).setWeight(5, 5));
        treeViewPanel.add(addNodeRadio, new GBC(0, 2).setWeight(5, 5));
        treeViewPanel.add(addChildRadio, new GBC(1, 2).setWeight(5, 5));
        treeViewPanel.add(addNodeButton, new GBC(0, 3).setWeight(5, 5));
        treeViewPanel.add(removeNodeButton, new GBC(1, 3).setWeight(5, 5));


        attributesScrollPane = new JScrollPane(attributesTable);
        attributesScrollPane.setPreferredSize(new Dimension(500, 400));

        attributesPanel.setLayout(gridBagLayout);
        attributesPanel.add(attributesScrollPane, new GBC(0, 0, 2, 1).setWeight(100, 100).setFill(GBC.BOTH));
        attributesPanel.add(attributeNameLabel, new GBC(0, 1).setWeight(1, 1).setAnchor(GBC.EAST).setInsets(5, 0, 5, 5));
        attributeNameTextF.setMinimumSize(new Dimension(150, 20));
        attributesPanel.add(attributeNameTextF, new GBC(1, 1).setWeight(1, 1).setAnchor(GBC.WEST).setInsets(5, 5, 5, 0));
        attributesPanel.add(attributeValueLabel, new GBC(0, 2).setWeight(1, 1).setAnchor(GBC.EAST).setInsets(5, 0, 5, 5));
        attributeValueTextF.setMinimumSize(new Dimension(150, 20));
        attributesPanel.add(attributeValueTextF, new GBC(1, 2).setWeight(1, 1).setAnchor(GBC.WEST).setInsets(5, 5, 5, 0));
        attributesPanel.add(addAttributeButton, new GBC(0, 3).setWeight(1, 1).setAnchor(GBC.EAST).setInsets(5, 0, 5, 20));
        attributesPanel.add(removeAttributeButton, new GBC(1, 3).setWeight(1, 1).setAnchor(GBC.WEST).setInsets(5, 40, 5, 0));


        swingTreeSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeViewPanel, attributesPanel);
        swingTreeSplitPane.setOneTouchExpandable(true);
        swingTreeSplitPane.setContinuousLayout(true);


        tabbedPane.addTab("Visual tree editor", visualTreePanel);
        tabbedPane.addTab("Editor swing component", swingTreeSplitPane);

        this.add(tabbedPane);

        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        this.setSize(screenSize.width / 2, screenSize.height / 2);
        this.setLocation(screenSize.width / 4, screenSize.height / 4);
        this.setTitle("Hierarchy Chart Editor");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void addNodeSelectionListener(TreeSelectionListener tsListener) {
        treeView.addTreeSelectionListener(tsListener);
    }

    public WebServiceTreeNode getSelected() {
        return (WebServiceTreeNode) treeView.getLastSelectedPathComponent();
    }

    public static void setModels(TreeModel treeModel, TableModel tableModel) {
        myTreeModel = treeModel;
        myTableModel = tableModel;
    }

    public void setTreeModel(TreeModel treeModel) {
         treeView.setModel(treeModel);
    }

    public void setTableModel(TableModel tableModel) {
        attributesTable.setModel(tableModel);
    }

    public void setAddNodeButtonListener(ActionListener listener) {
        addNodeButton.addActionListener(listener);
    }

    public void setRemoveNodeButtonListener(ActionListener listener) {
        removeNodeButton.addActionListener(listener);
    }

    public void setAddAttributeButtonListener(ActionListener listener) {
        addAttributeButton.addActionListener(listener);
    }

    public void setRemoveAttributeButtonListener(ActionListener listener) {
        removeAttributeButton.addActionListener(listener);
    }

    public boolean getAddNodeRadioState() {
        return addNodeRadio.isSelected();
    }

    public boolean getAddChildRadioState() {
        return addChildRadio.isSelected();
    }

    public String getNodeNameFieldText() {
        return nodeNameTextF.getText();
    }
}
